var paymentCtrl = $.mvc.controller.create('payment', {
    views: {
        "payment_tpl": "views/payment.tpl"
    },
    /* Array of views to load */
    save: function() {
        value = $("#new-todo").val();
        $("#new-todo").val('');
        var that = this;
        var todo = new Todo();
        if(value.length == 0) return alert("Please enter some text");
        todo.text = value;

        todo.set({
            text: value
        });
        todo.save(function() {
            //var str = "<li><input id='"+todo.id+"' data-id='" + todo.id + "' class='check' type='checkbox' value='active' /><label for='"+todo.id+"'/><div class='todo-text' >" + todo.text + "</div> <span class='todo-destroy' style='display:none;'><input data-id='" + todo.id + "' type='button' class='handleArchive' value='archive' style='width: 90px;height: 43px;' /></span></li>";
            var str=$.template("list_item",{'entry':todo});
            $("#leftTodo ul").append($(str));
            that.updateCounters();
        });
 
    },
    /* This function updates the number of todos on the screen */
    pay: function(code) {
        
        a = localStorage.getItem("userID");
         
        if(localStorage.getItem("user_event")){
              
             var id_number = $('#id_number').val();
          
        var carts = JSON.parse(localStorage.getItem('shopping_cart'));
        var status = false;
        var queryString = "";
        var eventpackageArray = [];
        if (carts.length > 0) {
            for(var i=0; i< carts.length; i++){
                eventpackageArray[i] = carts[i][0].eventpackageID;
            }
        } else {
            alert('Shopping cart is empty');
        }
             
              
        //checkout_win = window.open('', 'checkout_win');
        $.ajax({
            type:"POST",
            url:BASE_URL+"deal_payment",
            headers:{'X-Auth-Token':localStorage.getItem('access_token')},
             
            data:{userID:a, eventpackageArray:eventpackageArray, pay_method:code, id_number:'12345'},
            success:function(data){
                 
                localStorage.setItem('order_data', JSON.stringify(data));
                data = JSON.parse(data);
                data.email = 'anakpisces@gmail.com';
                
                 
                var redirect_url = '';
                var param = $.param(data);
                if (data.payMethod === 'ewallet') {
                     
                    redirect_url = BASE_URL2 + 'redirect_ewallet?'+param;
                } else if (data.payMethod === 'bca_klikpay') {
                    redirect_url = BASE_URL2 + 'redirect_klikpay?'+param;
                } else if (data.payMethod === 'credit_card') {
                    redirect_url = BASE_URL2 + 'redirect_cc?'+param;
                }
                checkout_win = window.open(redirect_url,'_blank','location=yes');
                 
                /*checkout_win.addEventListener('loadstart', function(event) {
                    if (event.url.match("mobile/close")) {
                        checkout_win.close();
                    }
                });*/
                
                 // success or not clean cart
                 
                localStorage.removeItem("shopping_cart");
                
                $.ui.loadContent("#main",false,false,"slide");
                
                
                checkout_win.addEventListener('exit', function(event) {
                    
                     
                    //alert('code:'+data.order_code);
                     
                    $.ajax({
                        type:"GET",
                        url:BASE_URL+"order?order_code="+data.order_code,
                        headers:{'X-Auth-Token':localStorage.getItem('access_token')},
                        //data:{order_code:data.order_code},
                        success:function(result){
                              
                            //alert(JSON.stringify(result));
                            result = JSON.parse(result);
                            //alert('result:'+result.paid);
                            if (result.paid === '1') {
                                localStorage.removeItem('order_data');
                                localStorage.removeItem('shopping_cart');
                            }
                            
                            $("#cart_div").html($.template('cart_tpl', {
                                items: null
                            }));
                            $("#totalCart").html("Total: Rp. 0");
                            $.ui.loadContent("#cart_panel",false,false,"slide");
                        },
                        error: function (xhr, ajaxOptions, thrownError) {

                        }
                    });
                });
            },
            error: function (xhr, ajaxOptions, thrownError) {

            }
        });
        
             
            }else{

             $.ui.loadContent("#select",false,false,"up");

        }
        
        
    },
    default:function() {
        payment.fetchAll(function(all) {
            $("#payment_div").html($.template('payment_tpl', {
                items: all
            }));
        });

    },
    /* This is executed when the controller is created.  It assumes the views are loaded, but can not interact with models
     * This is useful for wiring up page events, etc.
     */
    init: function() {
        
    }
});